$(function () {
    //下面是代码的抽取操作
    var employeeDatagrid,editAndQuitBtn,employeeDialog,editForm,rolebox;
    employeeDatagrid = $('#employee_datagrid');
    editAndQuitBtn = $('#editBtn,#quitBtn');
    employeeDialog = $('#employee_dialog');
    editForm = $('#editForm');
    rolebox=$("#employee_roles");
    employeeDatagrid.datagrid({
        url:'/employee/list.do',
        fit:true,
        fitColumns:true,
        pagination:true,
        pageNumber:1,
        pageSize:10,
        toolbar:"#tb",
        singleSelect:true,
        rownumbers:true,
        columns:[[
            {field:'username',title:'用户名',width:100,align:"center"},
            {field:'realName',title:'真实姓名',width:100,align:"center"},
            {field:'tel',title:'联系方式',width:100,align:"center"},
            {field:'email',title:'邮箱',width:100,align:"center"},
            {field:'department',title:'所属部门',width:100,align:"center",formatter:deptFormatter},
            {field:'inputTime',title:'入职时间',width:100,align:"center"},
            {field:'state',title:'状态',width:100,align:"center",formatter:stateFormatter},
            {field:'admin',title:'是否管理员',width:100,align:"center",formatter:adminFormatter}
        ]],
        onSelect:function (index,row) {
            if (row.state == -1) {
                //在这里是把组建都禁止
                editAndQuitBtn.linkbutton("disable");
            } else {
                editAndQuitBtn.linkbutton("enable");
            }
        }
    });
    var comdObj = {
        add:function () {
            employeeDialog.dialog("open");
            employeeDialog.dialog("setTitle","员工新增");
            editForm.form("clear");
        },
        cancel:function () {
            employeeDialog.dialog("close");
        },
        save:function () {
            //1.异步的提交表单
            editForm.form("submit",{
                url:"/employee/saveOrUpdate.do",
                onSubmit:function (param) {
                    var roles = rolebox.combobox("getValues");
                    for(var index = 0;index<roles.length;index++){
                        param["roles["+index+"].id"] = roles[index];
                    }
                },
                success:function (data) {
                    data = $.parseJSON(data);
                    if (data.success){
                        $.messager.alert("温馨提示:",data.msg,"info",function () {
                            employeeDatagrid.datagrid("reload");
                            employeeDialog.dialog("close");
                        })
                    }else {
                        $.messager.alert("温馨提示:",data.msg,"error");
                    }
                }
            });
        },
        edit:function () {
            var rowData = employeeDatagrid.datagrid("getSelected");
            if(!rowData){
                $.messager.alert("温馨提示:","请选中要编辑的信息","info");
            }else{
                editForm.form("clear");

                //获取到选中的数据rowData,基于同名匹配的规则,使用表单的load方法去加载数据
                if(rowData.department){
                    //如果存在部门对象的话,那么转化为id
                    rowData["department.id"]=rowData.department.id;
                }
                //rowData:是一个对象
                var roleIds = $.map(rowData.roles,function (value,index) {
                    return value.id;
                });
                rolebox.combobox("setValues",roleIds);
                //加载,实现了同名匹配的原则
                editForm.form("load",rowData);
                employeeDialog.dialog("setTitle","员工编辑");
                employeeDialog.dialog("open");
            }
        },
        remove:function () {
            var rowData = employeeDatagrid.datagrid("getSelected");
            if(!rowData){
                $.messager.alert("温馨提示:","请选中要离职的员工","info");
            }else{
                $.messager.confirm("温馨提示:","亲,确定要离职该员工",function (flag) {
                    if(flag){
                        $.get("/employee/updateState.do",{id:rowData.id},function (data) {
                            if(data.success){
                                $.messager.alert("温馨提示:",data.msg,"info",function () {
                                    //刷新当前的页面
                                    employeeDatagrid.datagrid("reload");
                                });
                            }else {
                                $.messager.alert("温馨提示:",data.msg,"error");
                            }
                        });
                    }
                });
            }
        },
        reload:function () {
            employeeDatagrid.datagrid("reload");
        }
    }
    employeeDialog.dialog({
        width: 400,
        height: 300,
        closed: true,
        buttons:"#dialog_bt"
    });
    $("a[data-cmd]").click(function () {
        var op = $(this).data("cmd");
        comdObj[op]();
    });
});
function deptFormatter(val) {
    if(val){
     return val.name;
    }
};
function stateFormatter(val) {
    if(val==0){
     return "<span style='color: green'>正常</span>";
    }
    return "<span style='color: red'>离职</span>";
};
function adminFormatter(val) {
    if(val){
     return "是";
    }
    return "否";
};

